package org.example.day20240905.util;

import com.mysql.jdbc.Driver;

import java.sql.*;

public class JDBCUtils {
    static Connection connection;
    static {
        try{
            DriverManager.registerDriver(new Driver());
            String url = "jdbc:mysql://localhost:3306/realworld1_db?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
            String username = "root";
            String pass = "123456";
            connection = DriverManager.getConnection(url,username,pass);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private JDBCUtils(){

    }

    public static Connection getConnection(){
        return connection;
    }

    public static void close(Connection connection
            , Statement statement){
        try {
            statement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Connection connection
            , Statement statement
            , ResultSet resultSet){
        try {
            resultSet.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
